package org.jeecg.modules.xj.utils;

import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.geom.RoundRectangle2D;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.nio.file.Path;
import java.util.HashMap;
import java.util.Map;
 
import javax.imageio.ImageIO;
 
import com.google.zxing.BarcodeFormat;
import com.google.zxing.EncodeHintType;
import com.google.zxing.MultiFormatWriter;
import com.google.zxing.WriterException;
import com.google.zxing.client.j2se.MatrixToImageConfig;
import com.google.zxing.client.j2se.MatrixToImageWriter;
import com.google.zxing.common.BitMatrix;
import com.google.zxing.datamatrix.encoder.SymbolShapeHint;
import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel;
 
/**
 * https://blog.csdn.net/p812438109/article/details/100165649
 *
 * 二维码工具类
 * @author ouyangjun
 */
public class QrcodeUtils {
 
    /**
     * 二维码原图
     * @param content: 二维码内容
     * @param width: 二维码宽度,不能小于0
     * @param height: 二维码高度,不能小于0
     * @param format: 二维码图片类型
     * @param path: 生成的二维码存放地址
     * @return
     * @throws WriterException
     * @throws IOException
     */
    public static BitMatrix qrcodeMasterMap(String content, int width, int height, String format, Path path) throws WriterException, IOException {
		
        Map<EncodeHintType, Object> hints = new HashMap<EncodeHintType, Object>();
        // 内容编码
        hints.put(EncodeHintType.CHARACTER_SET, "UTF-8");
        // 容错级别,图片像素越大,容错率可以设置低一点,图片像素越小,容错率要设置高一点
        hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.M);
        // 二维码边的空度
        hints.put(EncodeHintType.MARGIN, 1);
        // 矩形形状,默认是正方形
        hints.put(EncodeHintType.DATA_MATRIX_SHAPE, SymbolShapeHint.FORCE_SQUARE);
		
        BitMatrix bitMatrix = new MultiFormatWriter().encode(content, BarcodeFormat.QR_CODE, width, height, hints);
        // 把二维码原图片输出到指定的路径下
        MatrixToImageWriter.writeToPath(bitMatrix, format, path);
        
        // 返回
        return bitMatrix;
    }
 
    /**
     * 带logo的二维码,条形码
     * @param bitMatrix: 原图片信息
     * @param format: 图片类型
     * @param logoFile: logo图片地址
     * @param qrcodeFile: 生成的码图片存放地址
     * @throws WriterException
     * @throws IOException
     */
    public static void logoQrcode(BitMatrix bitMatrix, String format, File logoFile, File qrcodeFile) throws WriterException, IOException {
        // 图片颜色设置
        MatrixToImageConfig matrixToImageConfig = new MatrixToImageConfig(0xFF000001, 0xFFFFFFFF);
        
        BufferedImage bufferedImage = logoBufferedImage(MatrixToImageWriter.toBufferedImage(bitMatrix, matrixToImageConfig), logoFile);
        //输出带logo图片
        ImageIO.write(bufferedImage, format, qrcodeFile);
    }
	
    /**
     * 添加logo
     * @param bufferedImage 图片
     * @param logoFile logo图片文件
     * @return 返回带有logo的图片
     */
    private static BufferedImage logoBufferedImage(BufferedImage bufferedImage, File logoFile) throws IOException { 
        // 读取二维码图片，并构建绘图对象
        Graphics2D graphics2D = bufferedImage.createGraphics();
 
        // 图片像素
        int bufferedWidth = bufferedImage.getWidth();
        int bufferedHeigh = bufferedImage.getHeight();
 
        // 开始绘制图片, 并设置笔画对象
        graphics2D.drawImage(ImageIO.read(logoFile), bufferedWidth/5*2, bufferedHeigh/5*2, bufferedWidth/5, bufferedHeigh/5, null);
        BasicStroke basicStroke = new BasicStroke(5, BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND);
        graphics2D.setStroke(basicStroke);
        
        // 指定弧度的圆角矩形
        RoundRectangle2D.Float roundRectangle2D = new RoundRectangle2D.Float(bufferedWidth/5*2, bufferedHeigh/5*2, bufferedWidth/5, bufferedHeigh/5,20,20);
        graphics2D.setColor(Color.white);
        // 绘制圆弧矩形
        graphics2D.draw(roundRectangle2D);
 
        // 设置logo 有一道灰色边框, 并设置笔画对象
        BasicStroke logoBasicStroke = new BasicStroke(1, BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND);
        graphics2D.setStroke(logoBasicStroke);
        
        RoundRectangle2D.Float logoRoundRectangle2D = new RoundRectangle2D.Float(bufferedWidth/5*2+2, bufferedHeigh/5*2+2, bufferedWidth/5-4, bufferedHeigh/5-4, 20, 20);
        graphics2D.setColor(new Color(128,128,128));
        // 绘制圆弧矩形
        graphics2D.draw(logoRoundRectangle2D);
        
        graphics2D.dispose();
        bufferedImage.flush();
        
        // 返回
        return bufferedImage ;
    }
 
    /**
     * 条形码原图
     * @param content: 条形码内容
     * @param width: 条形码宽度,不能小于0
     * @param height: 条形码高度,不能小于0
     * @param format: 条形码图片类型
     * @param path: 条形码存放地址
     * @return
     * @throws WriterException
     * @throws IOException
     */
    public static BitMatrix barcode(String content, int width, int height, String format
			, Path path) throws WriterException, IOException {
    	
        HashMap<EncodeHintType, Object> hints = new HashMap<EncodeHintType, Object>();
        hints.put(EncodeHintType.CHARACTER_SET, "UTF-8");
        
        // 条形码的格式: BarcodeFormat.EAN_13
        BitMatrix bitMatrix = new MultiFormatWriter().encode(content, BarcodeFormat.EAN_13, width, height, hints);
        
        // 把二维码原图片输出到指定的路径下
        MatrixToImageWriter.writeToPath(bitMatrix, format, path);
        
        // 返回
        return bitMatrix;
    }
}